package com.restkeeper.gateway.config.customCode.utils;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;

import static com.restkeeper.gateway.config.customCode.config.CharConfig.encodedStr;
import static com.restkeeper.gateway.config.customCode.config.CustomCodeConfig.encoded;
import static com.restkeeper.gateway.config.customCode.config.CustomCodeConfig.formatCode;


public class CustomCodeUtils {

    public static String getCustomDecode(String code){
        if (StringUtils.isBlank(code)){
            return "";
        }
        String str = formatCode(code);
        JSONObject jsonObject = JSONObject.parseObject(str);
        if (jsonObject!=null){
            return (String)jsonObject.get("ip");
        }
        return code;
    }

    public static String getCustomEncoded(String ip){
        if (StringUtils.isBlank(ip)){
            throw new NullPointerException("ip is null");
        }
        return encoded(encodedStr(ip));
    }


}
